package vashion.azeroth.back.controller.item;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.contant.ForestErrorEnum;
import vashion.azeroth.contant.ItemErrorEnum;
import vashion.azeroth.contant.item.PropertyTypeEnum;
import vashion.azeroth.core.item.pojo.ForestPropertyDO;
import vashion.azeroth.core.item.pojo.PropertyValueDO;
import vashion.azeroth.core.manager.forest.BaseForestPropertyManager;
import vashion.azeroth.core.manager.forest.BasePropertyManager;
import vashion.azeroth.core.manager.forest.BasePropertyValueManager;
import vashion.azeroth.core.result.ApiResult;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 属性维护
 *
 * @param <T>
 */
@Controller
@RequestMapping("/back/propertyvalue")
public class BackPropertyValueController<T> extends BaseController {

    private final static Logger log = LoggerFactory.getLogger(BackPropertyValueController.class);

    @Autowired
    private BasePropertyValueManager basePropertyValueManager;

    @Autowired
    private BasePropertyManager basePropertyManager;
    @Autowired
    private BaseForestPropertyManager baseForestPropertyManager;


    @RequestMapping("publishPropertyValue.htm")
    @ResponseBody
    public ApiResult<Long> publishPropertyValue(@ModelAttribute("PropertyValueDO") PropertyValueDO propertyValueDO,
                                                HttpServletRequest request) {
        return basePropertyValueManager.publishPropertyValue(propertyValueDO, getLoginUserId(request));
    }

    @RequestMapping("deletePropertyValue.htm")
    @ResponseBody
    public ApiResult<Boolean> deletePropertyValue(@RequestParam(value = "id") Long id, HttpServletRequest request) {

        PropertyValueDO propertyValueDO = new PropertyValueDO();
        propertyValueDO.setId(id);
        int removeCount = basePropertyValueManager.remove(propertyValueDO, getLoginUserId(request));
        if (removeCount == 1) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(), ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
    }

    /**
     * 获取属性列表
     *
     * @return list中有两个元素，第一个为sku，第二个为其他属性列表
     */
    @RequestMapping("getForestPropertyValueList.htm")
    @ResponseBody
    public ApiResult<List<Map<String, List<PropertyValueDO>>>> getForestPropertyValueList(@RequestParam(value = "forestId") Long forestId) {

        if (null == forestId) {
            return ApiResult.newErrorResult(ForestErrorEnum.ARG_ERROR.getErrorCode(), ForestErrorEnum.ARG_ERROR.getErrorMessage());
        }
        ForestPropertyDO queryDO = new ForestPropertyDO();
        queryDO.setForestId(forestId);
        queryDO.setPageSize(200);
        List<ForestPropertyDO> forestPropertyDOList = baseForestPropertyManager.find(queryDO);

        if (CollectionUtils.isEmpty(forestPropertyDOList)) {
            return ApiResult.newErrorResult(ForestErrorEnum.ARG_ERROR.getErrorCode(), "改类目没有绑定任何属性");
        }
        Map<Long, ForestPropertyDO> propertyNameMap = Maps.newHashMap();
        for (ForestPropertyDO forestPropertyDO1 : forestPropertyDOList) {
            propertyNameMap.put(forestPropertyDO1.getPropertyId(), forestPropertyDO1);
        }
        ApiResult<List<PropertyValueDO>> propertyValueListResult = basePropertyValueManager.getFromMetaIdList(new ArrayList<Long>(propertyNameMap.keySet()), 1, 100);
        if (!propertyValueListResult.isSuccess()) {
            return ApiResult.newErrorResult(ForestErrorEnum.QUERY_ERROR.getErrorCode(), ForestErrorEnum.QUERY_ERROR.getErrorMessage());
        }
        Map<String, List<PropertyValueDO>> skuMap = new TreeMap<String, List<PropertyValueDO>>();
        Map<String, List<PropertyValueDO>> otherMap = new HashMap<String, List<PropertyValueDO>>();
        for (PropertyValueDO valueDO : propertyValueListResult.getData()) {
            ForestPropertyDO forestPropertyDO = propertyNameMap.get(valueDO.getPropertyMetaId());
            String title = forestPropertyDO.getPropertyTitle();
            if (forestPropertyDO.getPropertyType() == PropertyTypeEnum.SKU.getType()) {
                if (!skuMap.containsKey(title)) {
                    skuMap.put(title, new ArrayList<PropertyValueDO>());
                }
                skuMap.get(title).add(valueDO);
            } else {
                if (!otherMap.containsKey(title)) {
                    otherMap.put(title, new ArrayList<PropertyValueDO>());
                }
                otherMap.get(title).add(valueDO);
            }

        }
        //对sku的key进行排序
        List<Map<String, List<PropertyValueDO>>> resultList = Lists.newArrayList();
        resultList.add(skuMap);
        resultList.add(otherMap);
        return ApiResult.newSuccessResult(resultList);
    }

    /**
     * 获取属性值列表
     */
    @RequestMapping("getPropertyValueList.htm")
    @ResponseBody
    public ApiResult<List<PropertyValueDO>> getPropertyValueList(@RequestParam(value = "propertyId") Long propertyId,
                                                                 @RequestParam(value = "page") int page,
                                                                 @RequestParam(value = "pageSize") int pageSize) {

        ApiResult<List<PropertyValueDO>> propertyValueListResult = basePropertyValueManager.getFromMetaIdList(Lists.newArrayList(propertyId), page, pageSize);
        ApiResult<Integer> countResult = basePropertyValueManager.countPropertyValueList(Lists.newArrayList(propertyId));
        if (countResult.isSuccess()) {
            propertyValueListResult.setTotalCount(countResult.getData());
        }
        return propertyValueListResult;
    }

	/**
	 * 冻结一个属性值
	 *
	 * @return
	 */
    @RequestMapping("freezePropertyValue.htm")
    @ResponseBody
    public ApiResult<Boolean> freezePropertyValue(@RequestParam(value = "propertyValueId", required = true) long propertyValueId,
    		                              HttpServletRequest request) {
    	ApiResult<Boolean> freezeResult = basePropertyValueManager.freezePropertyValue(propertyValueId, getLoginUserId(request));
    	
    	if (freezeResult.isSuccess() && freezeResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(freezeResult.getErrorCode(), freezeResult.getErrorMsg());
        }
    }

    /**
	 * 恢复一个属性值
	 *
	 * @return
	 */
    @RequestMapping("resumePropertyValue.htm")
    @ResponseBody
    public ApiResult<Boolean> resumePropertyValue(@RequestParam(value = "propertyValueId", required = true) long propertyValueId,
    		                              HttpServletRequest request) {
    	ApiResult<Boolean> resumeResult = basePropertyValueManager.resumePropertyValue(propertyValueId, getLoginUserId(request));
    	
    	if (resumeResult.isSuccess() && resumeResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(resumeResult.getErrorCode(), resumeResult.getErrorMsg());
        }
    }

}
